<?php
require_once "modules/JSSDK.php";

class IndexAction extends BaseAction {
  public function test() {
    $trackUser = session('trackUser');

    if($trackUser) {
      $id = $trackUser['admin_id'];
      $result = M('Admin')->field('admin_id, admin_username, admin_nickname, admin_created')->find($id);
      if($result) {
        $ret['name'] = $result['admin_username'];
        $ret['avatar'] = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
        $ret['userid'] = $result['admin_id'];

        $con['notice_admin_id'] = $id;
        $con['notice_is_read'] = 0;
        $ret['notifyCount'] = M('Notice')->where($con)->count();
        
      }
    }

    echo dump($ret);

    $this->display();
  }

  private function _curlFetch($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    // if (1 == strpos("$".$host, "https://"))
    // {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    // }
    $response = curl_exec($curl);

    if (curl_errno($curl)) { 
      print curl_error($curl); 
    }

    $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $headerSize);
    $body = substr($response, $headerSize);
    return json_decode($body, true);
  }

  private function _getCode() {
    $code = $this->_get('code');
    $appid = 'wx87cb77e727af8680';
    if(!$code) {
      $redirectUrl = urlencode("https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirectUrl&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      header("Location: ".$url);
      exit;
    }else {
      return $code;
    }
  }

  private function _getToken($code) {
    $appid = 'wx87cb77e727af8680';
    $appsecret = 'f5d067559835de27da9fb1d87c8888d3';
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
    return $this->_curlFetch($url);
  }

  private function _getWechatUser($id) {
    $wechatUser = session('wechatUser');

    if($wechatUser) {
      return $wechatUser;
    }else {
      $code = $this->_getCode();
      $tokenData = $this->_getToken($code);
      $token = $tokenData['access_token'];
      $openId = $tokenData['openid'];

      $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openId&lang=zh_CN";
      $wechatUser = $this->_curlFetch($url);

      session(array('expire'=>7200));
      session('wechatUser', $wechatUser);

      header("Location: https://".$_SERVER['HTTP_HOST']."/track/index.php/Index/".$id);
      exit;
    }
  }

  public function _empty($id){
    $wechatUser = $this->_getWechatUser($id);

    $typeMap = array('1' => 'video' , '2' => 'file', '3' => 'photo');
    $data = parent::_modelDetail('PostView', 'post_id', $id);

    $data['post_thumb_url_full'] = 'https://'.$_SERVER['HTTP_HOST'].'/track/Uploads/thumb/'.$data['post_thumb_url'];

    $type = $typeMap[$data['post_type']];
    $data['post_created_time'] = date('Y-m-d', $data['post_created']);
    $data['post_file_url_full'] = '/track/Uploads/'.$type.'/'.$data['post_file_url'];

    if($type === 'file') {
      $htmlUrl = '/track/Uploads/'.$type.'/'.$data['post_file_url'].'.html';
      $htmlFilePath = $this->basePath.'Uploads/'.$type.'/'.$data['post_file_url'].'.html';
      if(file_exists($htmlFilePath)) {
        $data['post_file_url_html']= $htmlUrl;
      }
    }else if($type === 'video') {
      $posterUrl = '/track/Uploads/poster/'.$data['post_file_url'].'.jpg';
      $posterPath = $this->basePath.'Uploads/poster/'.$data['post_file_url'].'.jpg';
      if(file_exists($posterPath)) {
        $data['post_video_poster'] = $posterUrl;
      }
    }

    $this->assign('nickname', $wechatUser['nickname']);
    $this->assign('openid', $wechatUser['openid']);
    $this->assign('avatar', $wechatUser['headimgurl']);

    // $this->assign('nickname', '测试用户');
    // $this->assign('openid', '123456');
    // $this->assign('avatar', 'http://dsaadssdsa.jpg');

    $jssdk = new JSSDK("wx87cb77e727af8680", "f5d067559835de27da9fb1d87c8888d3", false);
    $signPackage = $jssdk->GetSignPackage();

    $this->assign('signPackage', $signPackage);
    $this->assign('data', $data);
    $this->assign('preview', $this->_get('preview'));
    $this->display('index');
  }
}
